<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
        content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <title>欣然之韵</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.3/css/bootstrap.min.css" rel="stylesheet">
</head>

<body>
    <div id="main-content" class="container" style="margin-top: 10px;">
        <h4>高考志愿填报辅助工具</h4>
        <br />
        <div class="row">
            <div class="col-md-12">
                <div class="form-group">
                    <label>高校名称：</label>
                    <input type="text" id="name" class="form-control" placeholder="高校名称">
                </div>
                <div class="form-group">
                    <label>高校城市：</label>
                    <input type="text" id="city" class="form-control" placeholder="高校城市">
                </div>
                <div class="form-group">
                    <label><span class="lastYear"></span>年录取最低分数：</label>
                    <input type="text" id="min21" class="form-control" placeholder="综合分(总分)">
                </div>
                <div class="form-group">
                    <label><span class="lastYear"></span>年录取最高分数：</label>
                    <input type="text" id="max21" class="form-control" placeholder="综合分(总分)">
                </div>
                <div class="form-group">
                    <button class="btn btn-primary" type="button" onclick="calc();">开始计算</button>
                    <button class="btn btn-primary" type="button" onclick="bubble();">冒泡排序</button>
                    <button class="btn btn-danger" type="button" onclick="deleteInput();">清空输入</button>
                    <button class="btn btn-danger" type="button" onclick="deleteCache();">删除缓存</button>
                </div>
            </div>
        </div>
        <br />
        <div>
            <div class="form-group">
                <label><span class="lastYear"></span>年省控线分数：</label>
                <input type="text" id="std21" class="form-control" placeholder="综合分(总分)">
            </div>
            <div class="form-group">
                <label><span id="thisYear"></span>年省控线分数：</label>
                <input type="text" id="std22" class="form-control" placeholder="综合分(总分)">
            </div>
            <div class="form-group">
                <label>个人综合分(个人总分)：</label>
                <input type="text" id="current" class="form-control" placeholder="文3专7">
            </div>
            <div class="form-group">
                <label>量化公式：</label>
                <input type="text" id="expression" class="form-control" readonly>
            </div>
        </div>
        <br />
        <div class="row">
            <div class="col-md-12">
                <h5 class="page-header" style="font-weight: bold">计算结果：</h5>
                <table class="table table-striped">
                    <tbody id="result"></tbody>
                </table>
            </div>
        </div>
    </div>
</body>
<script>
    var STD21 = 500;
    var STD22 = 505;
    var CURRENT = 550;

    window.onload = function () {
        $(".lastYear").html(lastYear());
        $("#thisYear").html(thisYear());
        $("#expression").val(expression());
        let cache = localStorage.getItem('cache');
        if (cache == null) {
            return;
        }
        let array = JSON.parse(cache);
        actualBubble(array);
        for (let index in array) {
            let data = array[index];
            let text = buildText(data.name, data.city,
                data.min21, data.max21,
                data.minExcept, data.maxExcept,
                data.minDiff, data.maxDiff);
            $("#result").append("<tr><td>" + text + "</td></tr>");
        }
    }

    function calc() {
        let name = $("#name").val();
        if (name === '') {
            alert('空值异常');
            return;
        }
        let city = $("#city").val();
        let min21 = $("#min21").val();
        let max21 = $("#max21").val();
        let std21 = $("#std21").val();
        if (isEmpty(std21)) {
            std21 = STD21;
        }
        let std22 = $("#std22").val();
        if (isEmpty(std22)) {
            std22 = STD22;
        }
        let current = $("#current").val();
        if (isEmpty(current)) {
            current = CURRENT;
        }
        let minExcept = round(min21 / std21 * std22);
        let flag = isEmpty(max21);
        let maxExcept;
        if (!flag) {
            maxExcept = round(max21 / std21 * std22);
        }
        let minDiff = calcDiff(current, minExcept);
        let maxDiff = flag ? null : calcDiff(current, maxExcept);
        let test = buildText(name, city, min21, max21, minExcept, maxExcept, minDiff, maxDiff);
        $("#result").append("<tr><td>" + test + "</td></tr>");
        let data = {
            "name": name, "city": city,
            "min21": min21, "max21": max21,
            "std21": std21, "std22": std22,
            "current": current,
            "minExcept": minExcept, "maxExcept": maxExcept,
            "minDiff": minDiff, "maxDiff": maxDiff
        };
        let cache = localStorage.getItem('cache');
        let array;
        if (cache == null) {
            array = [];
        } else {
            array = JSON.parse(cache);
        }
        array.push(data);
        localStorage.setItem('cache', JSON.stringify(array));
    }

    function bubble() {
        window.location.reload();
    }

    function deleteInput() {
        $("#name").val('');
        $("#city").val('');
        $("#min21").val('');
        $("#max21").val('');
    }

    function deleteCache() {
        localStorage.removeItem('cache');
        bubble();
    }

    function isEmpty(object) {
        if (object === null || object === undefined) {
            return true;
        }
        let type = typeof object;
        switch (type) {
            case "number":
                return object === 0;
            case 'string':
                return object.length === 0;
            default:
                return false;
        }
    }

    function round(int) {
        int = int * 1;
        return Math.round(int * 10) / 10;
    }

    function calcDiff(current, except) {
        return round(current * 1 - except * 1);
    }

    function buildText(name, city, min21, max21, minExcept, maxExcept, minDiff, maxDiff) {
        let flag = isEmpty(max21);
        let history = flag ? min21 : min21 + ' ~ ' + max21;
        let except = flag ? minExcept : minExcept + ' ~ ' + maxExcept;
        let diff = flag ? setStyle(minDiff) : setStyle(minDiff) + ' ~ ' + setStyle(maxDiff);
        return name + ' - ' + city + ' # [' + history + '] * [' + except + ']' + ' >> [' + diff + ']';
    }

    function setStyle(diff) {
        return '<span style=\'color: ' + (diff < 0 ? 'red' : 'green') + '\'>' + diff + '</span>';
    }

    function actualBubble(array) {
        let length = array.length;
        for (let i = 0; i < length - 1; i++) {
            for (let j = 0; j < length - 1; j++) {
                if (array[j].minDiff < array[j + 1].minDiff) {
                    let temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
    }

    function expression() {
        return thisYear() + "预期值=" + lastYear() + "录取分/" + lastYear() + "省控线*" + thisYear() + "省控线";
    }

    function thisYear() {
        return new Date().getFullYear();
    }

    function lastYear() {
        return thisYear() - 1;
    }
</script>

<!-- 恭喜你，看到了这一段注释，在这里，预祝你考上自己心目中最理想的高校！大学是我们人生中最美好的一段时光之一！希望你可以不负韶华！努力拼搏！ -->
<!-- 天之蓝 2022年6月27日 长沙 -->

</html>